// token.service.ts
import { Injectable } from '@nestjs/common';
import * as jwt from 'jsonwebtoken';

interface Token {
  userName: string;
  role: { key: string; name: string }[];
}

@Injectable()
export class TokenService {
  private readonly secretKey = 'betterme'; // 替换为你自己的密钥
  private readonly expiresIn = 60 * 60; // 过期时间
  sign(payload: any): string {
    return jwt.sign(payload, this.secretKey, { expiresIn: this.expiresIn });
  }
  decoded(token: string): Promise<Token> {
    return new Promise((resolve, reject) => {
      try {
        const decoded = jwt.verify(token, this.secretKey);
        resolve(decoded);
      } catch (err) {
        reject(err);
      }
    });
  }
}
